/*
 *  CEListItem.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/controls/CEListItem.h"

#include "cocos2dx_extentions/utility/CECocos2dUtility.h"

using namespace cocos2d;

namespace cocosext {


/////////////////////////////////////////////////////
//
/** CEListItem 
* 
*/
/////////////////////////////////////////////////////

CEListItem * CEListItem::itemFromNormalSprite(const CEListItemInfo& info, CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
{
    return CEListItem::itemFromNormalSprite(info, normalSprite, selectedSprite, disabledSprite, NULL, NULL);
}
CEListItem * CEListItem::itemFromNormalSprite(const CEListItemInfo& info, CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector)
{
    return CEListItem::itemFromNormalSprite(info, normalSprite, selectedSprite, NULL, target, selector);
}
CEListItem * CEListItem::itemFromNormalSprite(const CEListItemInfo& info, CCNode *normalSprite, CCNode *selectedSprite, CCNode *disabledSprite, CCObject *target, SEL_MenuHandler selector)
{
    CEListItem *pRet = new CEListItem(info);
	CCAssert(pRet != NULL, NULL);
	if (pRet->initFromNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector))
	{
		pRet->autorelease();
        return pRet;
	}
	else
	{
		CC_SAFE_DELETE(pRet)
		return NULL;
	}
}

/////////////////////////////////////////////////////
// Constructors and init
  
bool CEListItem::initFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector)
{
	if (!CEMenuItemSprite::initFromNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector))
		return false;

	return true;
}


/////////////////////////////////////////////////////
// Main interface

void CEListItem::setWidth(float width)
{
	CCSize sz = this->getContentSize();

	if (fabs(sz.width*this->getScaleX() - width) < 1.f)
		return;

	sz.width = width/this->getScaleX();
	this->setContentSize(sz);

	if (m_pNormalImage != NULL)
	{
		CCSize itemSz = m_pNormalImage->getContentSize();
		itemSz.width = sz.width/m_pNormalImage->getScaleX();
		m_pNormalImage->setContentSize(itemSz);
		m_pNormalImage->setPosition(ccpMid(sz));
	}
	if (m_pSelectedImage != NULL)
	{
		CCSize itemSz = m_pSelectedImage->getContentSize();
		itemSz.width = sz.width/m_pSelectedImage->getScaleX();
		m_pSelectedImage->setContentSize(itemSz);
		m_pSelectedImage->setPosition(ccpMid(sz));
	}
	if (m_pDisabledImage != NULL)
	{
		CCSize itemSz = m_pDisabledImage->getContentSize();
		itemSz.width = sz.width/m_pDisabledImage->getScaleX();
		m_pDisabledImage->setContentSize(itemSz);
		m_pDisabledImage->setPosition(ccpMid(sz));
	}
}

/////////////////////////////////////////////////////
// Protected interface
void CEListItem::onAfterAdd(CEVerticalListView* list)
{ 
	CCAssert(_list == NULL, "CEListItem::onAfterAdd: Dublication of adding to list!");
	_list = list;	
}
void CEListItem::onBeforeRemove() 
{ 
	CCAssert(_list != NULL, "CEListItem::onBeforeRemove: Item is not in list!");
	_list = NULL;	
}






/////////////////////////////////////////////////////
//
/** CEListItemWithText 
* 
*/
/////////////////////////////////////////////////////

CEListItemWithText* CEListItemWithText::itemFromNormalSprite(const CEListItemInfo& info,
															 cocos2d::CCNode* normalSpriteWithLabel, CELabelProtocol* pNormalLabel,
																		 cocos2d::CCNode* selectedSpriteWithLabel, CELabelProtocol* pSelectedLabel, 
																		 cocos2d::CCNode* disabledSpriteWithLabel, CELabelProtocol* pDisabledLabel, 
																		 cocos2d::CCObject* target, 
																		 cocos2d::SEL_MenuHandler selector)
{
    CEListItemWithText *pRet = new CEListItemWithText(info);
    pRet->initFromNormalSprite(normalSpriteWithLabel, pNormalLabel,
							   selectedSpriteWithLabel, pSelectedLabel, 
							   disabledSpriteWithLabel, pDisabledLabel, 
							   target, 
							   selector); 
    pRet->autorelease();
    return pRet;
}

/////////////////////////////////////////////////////
// Constructor, destructor and init
  
bool CEListItemWithText::initFromNormalSprite(cocos2d::CCNode* normalSpriteWithLabel, CELabelProtocol* pNormalLabel,
													cocos2d::CCNode* selectedSpriteWithLabel, CELabelProtocol* pSelectedLabel, 
													cocos2d::CCNode* disabledSpriteWithLabel, CELabelProtocol* pDisabledLabel, 
													cocos2d::CCObject* target, 
													cocos2d::SEL_MenuHandler selector)
{
	if (!CEListItem::initFromNormalSprite(normalSpriteWithLabel, selectedSpriteWithLabel, disabledSpriteWithLabel, target, selector))
		return false;

	_pNormalLabel = pNormalLabel;
	_pSelectedLabel = pSelectedLabel;
	_pDisabledLabel = pDisabledLabel;

	return true;
}

/////////////////////////////////////////////////////
//  CELabelProtocol interface 

void CEListItemWithText::setString(const wchar_t *str)
{
	_pNormalLabel->setString(str);
	_pSelectedLabel->setString(str);
	if (_pDisabledLabel != NULL)
		_pDisabledLabel->setString(str);
}

const wchar_t* CEListItemWithText::getString(void) const
{
	return _pNormalLabel->getString();
}

}; // end namespace cocosext

